package sniff.diller;

import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import sniff.pack.MyOSPFPacket;
import util.ByteUtil;
import util.InfoUtil;

public class OSPFPacketDiller {
	public static MyOSPFPacket dill(Packet pack) {
		byte[] head = pack.header;
		int startIndex=34;
		if(((IPPacket) pack).version == 4) {
			startIndex=34;
		}else if(((IPPacket) pack).version == 6){
			startIndex=54;
		}else {
			System.out.println("!!!!!!!!!!!!!!!error OSPFPacketDiller");
			return null;
		}
		int ospfVersion = ByteUtil.byteToInt(head[startIndex]);
		int packType = ByteUtil.byteToInt(head[startIndex+1]);
		int packLen = ByteUtil.unionByte(head[startIndex+2], head[startIndex+3]);
		long routerID = ByteUtil.unionByte(head[startIndex+4], head[startIndex+5], head[startIndex+6], head[startIndex+7]);
		long areaID = ByteUtil.unionByte(head[startIndex+8], head[startIndex+9], head[startIndex+10], head[startIndex+11]);
		String checkSum = "0x"+ByteUtil.byteToHexString(head[12])+ByteUtil.byteToHexString(head[13]);
		int auType = ByteUtil.unionByte(head[startIndex+14], head[startIndex+15]);
		String authentication = "";
		for(int i=16;i<24;i++) {
			authentication+=ByteUtil.byteToHexString(head[startIndex+i])+" ";
		}
		MyOSPFPacket ospfPack = new MyOSPFPacket(ospfVersion, packType, packLen, routerID, areaID, checkSum, auType, authentication);
		ospfPack.setInfo(InfoUtil.getOSPFInfo(ospfPack));
		return ospfPack;
	}
}
